package com.xiaoshuidi.cloud.module.contract.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractSmsConfigDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.ContractSmsConfigMapper;
import com.xiaoshuidi.cloud.module.rooms.api.roomhouse.RoomHouseApi;
import com.xiaoshuidi.cloud.module.rooms.vo.RoomHouseReserveVo;
import com.xiaoshuidi.cloud.module.system.api.sms.SmsSendApi;
import com.xiaoshuidi.cloud.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RequiredArgsConstructor
@TenantJob
public class SmsMessageRoomReserveJob {

    @Resource
    private ContractSmsConfigMapper contractSmsConfigMapper;
    @Resource
    private SmsSendApi smsSendApi;
    @Resource
    private RoomHouseApi roomHouseApi;

    @XxlJob("SmsMessageRoomReserveJob")
    @TenantIgnore
    public void execute(){
        log.info("SmsMessageBillJob预定短信推送开始");
        //查询配置
        List<ContractSmsConfigDO> contractSmsConfigDOS = contractSmsConfigMapper.selectList();
        if(CollectionUtil.isNotEmpty(contractSmsConfigDOS)){
            contractSmsConfigDOS.forEach(item->{
                Long apartmentId = item.getApartmentId();
                String tiem = DateUtil.format(DateUtil.offsetDay(new Date(),Integer.valueOf("-" + item.getBillPushDay())),"yyyy-MM-dd");

                List<RoomHouseReserveVo> roomHouseReserveVos = roomHouseApi.getRoomHouseReserve(tiem, apartmentId).getData();
                //存在
                if(CollectionUtil.isNotEmpty(roomHouseReserveVos)){
                    roomHouseReserveVos.forEach(itemReserve->{
                        Map<String, Object> map =  new HashMap<>();
                        // 获得渠道对应的 SmsClient 客户端
                        SmsSendSingleToUserReqDTO smsSendSingleToUserReqDTO = new SmsSendSingleToUserReqDTO();
                        map.put("customerName",itemReserve.getBooker());
                        map.put("customerMobile",itemReserve.getBookerPhone());
                        //短信渠道
                        smsSendSingleToUserReqDTO.setTemplateCode("reserve_ticket_reminders");

                        smsSendSingleToUserReqDTO.setMobile(itemReserve.getBookerPhone());
                        smsSendSingleToUserReqDTO.setUserId(itemReserve.getBookerId());
                        smsSendSingleToUserReqDTO.setTemplateParams(map);
                        smsSendApi.sendSingleSmsToMember(smsSendSingleToUserReqDTO);
                    });
                }
            });
        }
    }
}
